package com.zjq.springsocial.renren.api.impl.json;

import java.io.IOException;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.type.TypeReference;

import com.zjq.springsocial.renren.api.Work.Industry;
import com.zjq.springsocial.renren.api.Work.Job;

@JsonIgnoreProperties(ignoreUnknown = true)
abstract class WorkMixin {
	
	@JsonCreator
	WorkMixin(
			@JsonProperty("name") String name,// 公司名称
			@JsonProperty("time") String time,// 入职日期
			@JsonProperty("Industry") 
			@JsonDeserialize(using = IndustryDeserializer.class)
			Industry Industry,// 行业
			@JsonProperty("job") 
			@JsonDeserialize(using = JobDeserializer.class)
			Job job // 职位
	){}
	
	private class IndustryDeserializer extends JsonDeserializer<Industry> {
		@Override
		public Industry deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setDeserializationConfig(ctxt.getConfig());
			jp.setCodec(mapper);
			if (jp.hasCurrentToken()) {
				JsonNode dataNode = jp.readValueAsTree();
				if (dataNode != null) {
					return (Industry) mapper.readValue(dataNode,new TypeReference<Industry>(){});
				}
			}
			return null;
		}
	}
	
	private class JobDeserializer extends JsonDeserializer<Job> {
		@Override
		public Job deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setDeserializationConfig(ctxt.getConfig());
			jp.setCodec(mapper);
			if (jp.hasCurrentToken()) {
				JsonNode dataNode = jp.readValueAsTree();
				if (dataNode != null) {
					return (Job) mapper.readValue(dataNode,new TypeReference<Job>(){});
				}
			}
			return null;
		}
	}
	
}
@JsonIgnoreProperties(ignoreUnknown = true)
abstract class IndustryMixin{
	
	@JsonCreator
	IndustryMixin(
			@JsonProperty("industryCategory") String industryCategory,// 行业类别
			@JsonProperty("industryDetail") String industryDetail// 行业详情
    ) {}
}
@JsonIgnoreProperties(ignoreUnknown = true)
abstract class JobMixin{
	
	@JsonCreator
	JobMixin(
			@JsonProperty("jobCategory") String jobCategory,// 职位类别
			@JsonProperty("jobDetail") String jobDetail// 职位详情
	){}
}